package cn.itsy.web.controller;

import cn.itsy.dao.EmpDaoT;
import cn.itsy.dao.Pagedao;
import cn.itsy.pojo.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("dl")
public class DimissionTController {
    @Resource
    private EmpDaoT et;
    @Resource
    private Pagedao pd;

    @RequestMapping("list")
    public String list(HttpServletRequest request, HttpServletResponse response, String empname, String sex, String deptid, String ductid, String jobid, String paixu, String currentPage) {
        String sql = "select e.id,e.empid,e.empname,d.dname,e.sex,e.idcard,e.birthday,e.education,j.name,e.officephone from emp e,empstatus s,dept d,job j where e.is_status=s.id and e.depart_id=d.id and e.job_id=j.id and e.is_status = '3'";
        String sql2 = "select * from dept where 1=1";
        String sql4 = "select * from job where 1=1";
        Page page = new Page();
        if (empname != null && !empname.equals("")) {
            sql += " and e.empname like '%" + empname + "%' ";
        }
        if (sex != null && !sex.equals("")) {
            sql += " and e.sex like '%" + sex + "%' ";
        }
        if (deptid != null && !deptid.equals("")) {
            sql += " and e.depart_id = '" + deptid + "' ";
        }
        if (jobid != null && !jobid.equals("")) {
            sql += " and e.job_id = '" + jobid + "' ";
        }
        if (paixu != null && !paixu.equals("1")) {
            sql += " ORDER BY id asc ";
        }
        if (paixu != null && !paixu.equals("2")) {
            sql += " ORDER BY id desc ";
        }
        Page empTshowfy2 = pd.empTshowfy2(sql, currentPage, page);

        List<Dept> ml = et.deptAll(sql2);
        request.setAttribute("ml", ml);
        System.out.println(ml);

        /*List<Emp> el = et.queryAll(sql);
        request.setAttribute("el",el);*/

        List<Job> jl = et.JobAll(sql4);
        request.setAttribute("jl", jl);
        request.setAttribute("empTshowfy2", empTshowfy2);
        return "/DimissionT";
    }

    //删除
    @RequestMapping("del")
    public String del(HttpServletRequest request, HttpServletResponse response, String eid) {
        String sql = "delete from emp where 1=1 and id = '" + eid + "'";
        et.del(sql);
        return "redirect:list";
    }

    //去添加
    @RequestMapping("toadd")
    public String toadd(HttpServletRequest request, HttpServletResponse response) {
        String sql1 = "select * from dept where 1=1";
        List<Dept> deptAll = et.deptAll(sql1);
        request.setAttribute("deptAll", deptAll);

        String sql3 = "select * from job where 1=1";
        List<Job> jobAll = et.JobAll(sql3);
        request.setAttribute("jobAll", jobAll);

        String sql4 = "select * from empstatus where 1=1";
        List<Empstatus> statuses = et.statusAll(sql4);
        request.setAttribute("status", statuses);
        return "/DimissionTadd";
    }

    //添加
    @RequestMapping("DimissionTadd")
    public String add(HttpServletRequest request, HttpServletResponse response, Emp e) {
        String sqls = "select * from emp where empid ='" + e.getEmpid() + "'";
        List<Emp> empList = et.queryAll(sqls);

        if (empList.isEmpty()) {
            System.out.println("-----------------------可以添加");
            String sql = "INSERT INTO emp VALUES (null,'" + e.getEmpid() + "','" + e.getEmpname() + "'," +
                    "'" + e.getDepartId() + "','0','" + e.getEnglishname() + "','" + e.getNameusedbefore() + "'," +
                    "'" + e.getSex() + "','" + e.getIdcard() + "','" + e.getBirthday() + "','" + e.getNatives() + "','" + e.getNation() + "'," +
                    "'" + e.getPoleticalstatus() + "','" + e.getReligiousbelief() + "','" + e.getHealthcondition() + "',''," +
                    "'" + e.getMaritalstatus() + "','" + e.getWagesaccount() + "','" + e.getSocialsecuritynumbers() + "'," +
                    "'" + e.getPartworktime() + "','" + e.getEntertime() + "','" + e.getPlace() + "','" + e.getEducation() + "'," +
                    "'" + e.getJobId() + "','" + e.getOfficephone() + "','" + e.getHomephone() + "','" + e.getMobilephone() + "'," +
                    "'" + e.getIsStatus() + "')";
            et.add(sql);
            request.setAttribute("check", "success");
            return "redirect:list";
        } else {
            System.out.println("---------------------------重复了");
            request.setAttribute("check", "error");
            return "/DimissionTadd";
        }
    }

    //去修改
    @RequestMapping("toupdate")
    public String toupdate(HttpServletRequest request, HttpServletResponse response, String id) {
        String sql = "select * from emp where id='" + id + "'";
        List<Emp> emps = et.queryAll(sql);
        request.setAttribute("emps", emps);

        String sql1 = "select * from dept where 1=1";
        List<Dept> deptAll = et.deptAll(sql1);
        request.setAttribute("deptAll", deptAll);

        String sql3 = "select * from job where 1=1";
        List<Job> jobAll = et.JobAll(sql3);
        request.setAttribute("jobAll", jobAll);

        String sql4 = "select * from empstatus where 1=1";
        List<Empstatus> statuses = et.statusAll(sql4);
        request.setAttribute("status", statuses);

        return "/DimissionTupdate";
    }

    //修改
    @RequestMapping("DimissionTupdate")
    public String update(Emp e) {
        String sql = "update emp set empid='" + e.getEmpid() + "',empname='" + e.getEmpname() + "',depart_id='" + e.getDepartId() + "',sex='" + e.getSex() + "',idcard='" + e.getIdcard() + "',birthday='" + e.getBirthday() + "',education='" + e.getEducation() + "',job_id='" + e.getJobId() + "',officephone='" + e.getOfficephone() + "' where id='" + e.getId() + "'";
        et.update(sql);
        return "redirect:list";
    }

    //去修改编号
    @RequestMapping("Dimiss_Toupdate")
    public String Toupdate(Model model, HttpServletResponse response, String id) {
        String sql = "select * from emp where id='" + id + "' ";
        List<Emp> emps = et.queryAll(sql);
        model.addAttribute("es", emps);
        return "/Dimiss_toupid";
    }

    //修改编号
    @RequestMapping("Dimiss_toupid")
    public String EmpTtoupid(Emp e, String id) {
        String sql = "update emp set empid='" + e.getEmpid() + "' where id='" + e.getId() + "'";
        et.update(sql);
        return "redirect:list";
    }

    //查询不在职记录
    @RequestMapping("Dimiss_JiLu")
    public String EmpJiLu(HttpServletRequest request, HttpServletResponse response, String id) {
        String sql = " select e.id,e.empid,e.empname,d.dname,t.zname,e.sex,e.idcard,e.birthday,e.education,j.name,e.officephone,y.changetime,\n" +
                "y.changdesc,y.type,s.status from emp e,empstatus s,dept d,job j,duct t,emphistory y where e.is_status = s.id and e.depart_id = d.id and e.job_id = j.id and e.duct_id = t.id and y.emp_id = e.id and e.is_status = '3' and e.id='" + id + "'";
        List<Emp> eps = et.queryAll(sql);
        request.setAttribute("ep", eps);
        return "/Dimiss_JiLu";
    }
}
